#
# Copyright (c) 2024 Black Sesame Technologies
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

import os
import subprocess

def remove_trailing_slash(input_string):
    while input_string.endswith('/') or input_string.endswith('\\'):
        input_string = input_string[:-1]
    return input_string

def package(path):
    path = remove_trailing_slash(path)
    pkg_name = os.path.basename(path)

    current_dir = os.path.dirname(os.path.abspath(__file__))
    build_dir = os.path.join(current_dir, 'build')
    build_dir_abs = os.path.abspath(build_dir)

    cmake_dir = build_dir_abs + "/" + pkg_name

    cmd = "cd " + cmake_dir + " &&" + "rm -rf build/" + " && " + "mkdir build" + " && " + "cd build" + " && " + "cmake .." + " && " + "make -j2"
    ret = subprocess.run(cmd, shell=True, encoding="utf-8")
    if ret.returncode == 0:
        print("generating shared lib succeed.")
    else:
        print("generating shared lib failed.")
